<?php

    // 9/5/2009: immanuel192 create this class

    global $db;
    // because if the client switch page, so, that is the action and exponent does not transfer $title and $views		
    $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
    if ($temp)
    {
        $title=$temp->title;
        $view=$temp->view;
    }
    else
    {
        $title="";
        $view="Default";
    }
    // inital the template class 
    $template = new template("imagegallerymodule",$view,$loc);
    // get gallery config
    $config=$db->selectObject("imagegallerymodule_config","location_data = '".serialize($loc)."'");
    if (!$config) {
        $config->perpage = 15;
    }
    $max_item = $config->perpage;
    // get all gallery here
    $count_post = $db->countObjects('imagegallery_gallery',"location_data='".serialize($loc)."'");

    // page split
    // cal how many posts
    $page_count=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $page_count++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;

    $galleries = $db->selectObjects('imagegallery_gallery',"location_data='".serialize($loc)."' ORDER BY galleryorder DESC LIMIT {$start_pos},{$max_item}");
    $iloc = exponent_core_makeLocation($loc->mod,$loc->src);
    for ($i = 0; $i < count($galleries); $i++) {
        $iloc->int = $galleries[$i]->id;
        $galleries[$i]->permissions = array(
        'edit'=>exponent_permissions_check('edit',$iloc),
        'delete'=>exponent_permissions_check('delete',$iloc)
        );
        // count how many image in this gallery
        $temp=$db->countObjects("imagegallery_image","gallery_id = {$galleries[$i]->id}");
        $galleries[$i]->image_count = $temp;

        // get first image
        $temp=$db->selectObject("imagegallery_image","gallery_id = {$galleries[$i]->id}");
        $image=$temp;
        if ($image){
            $temp=$db->selectObject("file","id = {$image->file_id}");
            $file_path=$temp->directory ."/". $temp->filename;
            $galleries[$i]->image_path=$file_path;
        }
        else {$galleries[$i]->image_path="";}
    }

    //eDebug($galleries); exit();

    $template->assign('galleries',$galleries);
    $template->assign('page_count',$page_count);
    $template->assign('page',$page);
    $template->assign('moduletitle',$title);
    //$template->assign('show_desc',$config->show_pic_desc);
    $template->register_permissions(
    array('administrate','create','edit','delete','manage'),
    $loc);
    $template->output();

?>
